﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace XFSale.Aux
{
    public partial class TabFldPage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string tabName = string.Empty;
                if (Request["tabName"] != null)
                {
                    tabName = Request["tabName"];
                    ddlTabName.SelectedValue = tabName;
                }

                dsTab.SelectParameters.Clear();
                dsTab.SelectParameters.Add("tabName", tabName);
            }
        }

        protected void gvTab_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton btnEdit = e.Row.FindControl("btnEdit") as ImageButton;
                btnEdit.CommandArgument = Convert.ToString(e.Row.RowIndex);
            }
        }

        protected void gvTab_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Insert":
                    fvTab.InsertItemTemplate = fvTab.EditItemTemplate;
                    fvTab.ChangeMode(FormViewMode.Insert);
                    fvTab.Visible = true;
                    gvTab.Visible = false;
                    break;

                case "Edit":
                    fvTab.PageIndex = gvTab.PageIndex * gvTab.PageSize + Convert.ToInt32(e.CommandArgument);
                    fvTab.ChangeMode(FormViewMode.Edit);
                    fvTab.Visible = true;
                    gvTab.Visible = false;
                    break;
            }
        }

        protected void fvTab_PreRender(object sender, EventArgs e)
        {
            Button btnCtrl = null;
            TextBox txtCtrl = null;
            DropDownList ddlCtrl = null;
            RadioButtonList rblCtrl = null;
            DataRowView row = fvTab.DataItem as DataRowView;
            switch (fvTab.CurrentMode)
            {
                case FormViewMode.Edit:
                    btnCtrl = fvTab.FindControl("btnInsert") as Button;
                    if (btnCtrl != null)
                    {
                        btnCtrl.Visible = false;
                    }

                    txtCtrl = fvTab.FindControl("txtTabName") as TextBox;
                    txtCtrl.Enabled = false;
                    txtCtrl = fvTab.FindControl("txtFldName") as TextBox;
                    txtCtrl.Enabled = false;

                    if (fvTab.DataItem != null)
                    {
                        ddlCtrl = fvTab.FindControl("ddlFldType") as DropDownList;
                        ddlCtrl.SelectedValue = Convert.ToString(row["fldType"]);

                        rblCtrl = fvTab.FindControl("rblFldNull") as RadioButtonList;
                        rblCtrl.SelectedValue = Convert.ToString(row["fldNull"]);

                        rblCtrl = fvTab.FindControl("rblFldPK") as RadioButtonList;
                        rblCtrl.SelectedValue = Convert.ToString(row["fldPK"]);
                    }

                    break;

                case FormViewMode.Insert:
                    btnCtrl = fvTab.FindControl("btnUpdate") as Button;
                    if (btnCtrl != null)
                    {
                        btnCtrl.Visible = false;
                    }

                    txtCtrl = fvTab.FindControl("txtTabName") as TextBox;
                    txtCtrl.Enabled = false;
                    txtCtrl = fvTab.FindControl("txtFldName") as TextBox;
                    txtCtrl.Enabled = false;

                    break;
            }
        }

        protected void fvTab_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            DropDownList ddlFldType = fvTab.FindControl("ddlFldType") as DropDownList;
            RadioButtonList rblFldNull = fvTab.FindControl("rblFldNull") as RadioButtonList;
            RadioButtonList rblFldPK = fvTab.FindControl("rblFldPK") as RadioButtonList;

            e.NewValues["fldType"] = ddlFldType.SelectedValue;
            e.NewValues["fldNull"] = rblFldNull.SelectedValue;
            e.NewValues["fldPK"] = rblFldPK.SelectedValue;
        }

        protected void fvTab_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            ChangeToShow();
        }

        protected void fvTab_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            ChangeToShow();
        }

        protected void fvTab_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            DropDownList ddlFldType = fvTab.FindControl("ddlFldType") as DropDownList;
            RadioButtonList rblFldNull = fvTab.FindControl("rblFldNull") as RadioButtonList;
            RadioButtonList rblFldPK = fvTab.FindControl("rblFldPK") as RadioButtonList;

            e.Values["fldType"] = ddlFldType.SelectedValue;
            e.Values["fldNull"] = rblFldNull.SelectedValue;
            e.Values["fldPK"] = rblFldPK.SelectedValue;
        }

        protected void fvTab_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "Cancel".ToUpper())
            {
                ChangeToShow();
            }
        }

        protected void ddlTabName_SelectedIndexChanged(object sender, EventArgs e)
        {
            dsTab.SelectParameters.Clear();
            dsTab.SelectParameters.Add("tabName", ddlTabName.SelectedValue);
            gvTab.DataBind();
        }

        private void ChangeToShow()
        {
            fvTab.Visible = false;
            gvTab.Visible = true;
            gvTab.EditIndex = -1;
            pnlSearch.Visible = true;
        }

        protected void btnInsert_Click(object sender, ImageClickEventArgs e)
        {
            fvTab.InsertItemTemplate = fvTab.EditItemTemplate;
            fvTab.ChangeMode(FormViewMode.Insert);
            fvTab.Visible = true;
            gvTab.Visible = false;
        }
    }
}
